//
//  AnalysisViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/8/21.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

import SnapKit

class AnalysisViewController: BaseViewController,UIPageViewControllerDelegate,UIPageViewControllerDataSource,ChangePage {
    
    var father : NewAnalysisContainerViewController?
    
    var ctls: Array<UIViewController> = []
    
    private let page = UIPageViewController.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    
    @IBOutlet weak var trayView: SlidePageView!
    
    @IBOutlet weak var lineCenterX: NSLayoutConstraint!
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        if let idx = ctls.index(of: viewController) {
            
            if idx > 0 {
                
                return ctls[idx - 1]
                
            } else {
                
                return nil
                
            }
        }
        
        return nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        if let idx = ctls.index(of: viewController) {
            
            if idx < ctls.count - 1 {
                
                return ctls[idx + 1]
                
            } else {
                
                return nil
                
            }
        }
        
        return nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
            
            let vc = pageViewController.viewControllers![0]
            
            if let index = ctls.index(of: vc) {
                
                trayView.slide(toPosition: index)
            }
            
        }
    }
    
    func appendVC() -> Void {
        
        let saleVC = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "MarketingAnalysisViewController") as! MarketingAnalysisViewController
        saleVC.father = self.father
        
        ctls.append(saleVC)
        
        let saleVC1 = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "PurchasingAnalysisViewController") as! PurchasingAnalysisViewController
        saleVC1.father = self.father
        
        ctls.append(saleVC1)
        
        let HUMAN = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HumanResourceViewController") as! HumanResourceViewController
        
        HUMAN.father = self.father
        
        ctls.append(HUMAN)
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        for view in page.view.subviews {
            if view is UIScrollView {
                let scroll = view as! UIScrollView
                scroll.bounces = false
            }
        }
        
        trayView.delegate = self
        
        trayView.configTitles(["营销统计","采购统计","人资统计"])
        
        page.delegate = self
        
        page.dataSource = self
        
        view.addSubview(page.view)
        
        appendVC()
        
        page.setViewControllers([ctls[0]], direction: .forward, animated: false, completion: nil)
        
        for scroll in page.view.subviews {
            if scroll is UIScrollView {
                let s = scroll as! UIScrollView
                s.delegate = trayView
            }
        }
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        page.view.snp.makeConstraints { (make) in
            make.top.equalTo(55)
            make.bottom.equalTo(view)
            make.left.equalTo(view)
            make.right.equalTo(view)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func change(toPage idx: Int) {
        
        let previousVC = self.page.viewControllers![0]
        
        let index = self.ctls.index(of: previousVC)!
        
        if idx < self.ctls.count {
            
            let vc = self.ctls[idx]
            
            if index > idx {
                page.setViewControllers([vc], direction: .reverse, animated: true, completion: nil)
            } else {
                page.setViewControllers([vc], direction: .forward, animated: true, completion: nil)
            }
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
